$(function(){

	//登录
    $('#login').click(function(){
        login();
    });

    $('#username, #password').keyup(function(event){
        if(event.keyCode == 13){
            login();
        }
    });

    var login = function(){
        if ($('#loginForm').valid()) {

            var data = {
                username: $('#username').val(),
                password: $.md5($('#password').val())
            };
            $.ajax({
                type: 'POST',
                url: '/loginAction',
                data:data,
                success: function (json) {
                    if(json.error){
                        alert(json.error);
                        return;
                    }
                    window.location.href = json.url;
                },
                error: function (e) {
                    alert(e);
                }
            });
        }
    };

    $('#loginForm').validate({
        rules: {
            username: 'required',
            password: 'required',
        },
        messages: {
            username: '用户名不能为空',
            password: '密码不能为空 ',
        },
        errorLabelContainer: $('#loginErr'),
        wrapper: "li"
    });

    if($('#loginErr').text()==''){
        $('#loginErr').hide();
    }

});